package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import model.Sensor;
import view.ServerViewPanelSensor;
import databaseController.DatabaseController;

public class ServerSensorController {

	private ServerViewPanelSensor panel;
	
	public ServerSensorController() {
		this.panel = new ServerViewPanelSensor();		
		this.panel.getjButtonConfirm().addActionListener(new ListenerConfirm());
		this.panel.getjButtonClear().addActionListener(new ListenerClear());
	}
	
	public ServerViewPanelSensor getPanel() {
		return this.panel;
	}
	
	private void clear() {
		ServerSensorController.this.panel.getjTextFieldCode().setText("");
		ServerSensorController.this.panel.getjTextFieldPlace().setText("");		
	}
	
	class ListenerConfirm implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			String code = ServerSensorController.this.panel.getjTextFieldCode().getText();
			String place = ServerSensorController.this.panel.getjTextFieldPlace().getText();			
			
			DatabaseController.getInstance().addSensor(new Sensor(code, place));
			
			JOptionPane.showMessageDialog(null,
				    "Sensor cadastrado com sucesso!\n " , 
				    "Sucesso", JOptionPane.INFORMATION_MESSAGE);
			
			ServerSensorController.this.clear();
			
			System.out.println("*=*   Sensor   *=*");
			System.out.println("Code: " + code);
			System.out.println("Place: " + place);
			System.out.println("*=*   ------------------   *=*");
		}
	}
	
	class ListenerClear implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			
			ServerSensorController.this.clear();
		}
	}	
}
